package org.dataone.dataUsage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/metric")
public class MetricController {
	@Autowired
	private String CN_URL;
	@RequestMapping(value = "/cn/{timespan}", method = RequestMethod.GET)
	/**
	 * This method is to control the model layer obtain the CN metric and return a view layer.
	 * Returns a jsp view layer for data visualization.
	 * @param tiemspan the time span for metric display
	 * @param model add model view for jsp */
	public String getCNMetrics(@PathVariable int timespan, Model model) {
		ArrayList<MonthlyMetric> total = new ArrayList<MonthlyMetric>();
		try {
			total = DataCache.INSTANCE.getCNDataMap(CN_URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ArrayList<String> result = new ArrayList<String>();
		for (int i = 24 - timespan; i < 24; i++) {
			result.add(total.get(i).getMetricValue());
		}
		DateList dl = new DateList(timespan);
		model.addAttribute("date", dl.getDatelist());
		model.addAttribute("data", result);
		return "CNode";
	}
	@RequestMapping(value = "/mn/{timespan}", method = RequestMethod.GET)
	/**
	 *This method is to get the Member Node Name List and generate the default Member Node figure.
	 *Returns a view layer for visualization.
	 *@param timespan the time span for metric display
	 *@param model add model view for jsp
	 **/
	public String getMemberNodeList(@PathVariable int timespan, Model model) {
		Map<String, ArrayList<MonthlyMetric>> MNNodeMap = new HashMap<String, ArrayList<MonthlyMetric>>();
		try {
			MNNodeMap = DataCache.INSTANCE.getMNDataMap(CN_URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		MNodeMetric mn = new MNodeMetric();
		mn.setCN_URL(CN_URL);
		ArrayList<String> mnodelist = new ArrayList<String>();
		mnodelist = mn.getMNodeList();
		model.addAttribute("mnodelist", mnodelist);

		ArrayList<MonthlyMetric> total = new ArrayList<MonthlyMetric>();
		total = MNNodeMap.get(mnodelist.get(0));

		ArrayList<String> result = new ArrayList<String>();
		for (int i = 24 - timespan; i < 24; i++) {
			result.add(total.get(i).getMetricValue());
		}
		DateList dl = new DateList(timespan);
		model.addAttribute(
				"mnode",
				"'"
						+ mnodelist.get(0).substring(10,
								mnodelist.get(0).length() - 1) + "'");
		model.addAttribute("date", dl.getDatelist());
		model.addAttribute("data", result);
		return "MNodeList";
	}
	/**
	 * This method is to obtain metric for a selected Member node. Returns a
	 * view layer for visualization.
	 * 
	 * @param timespan
	 *            the time span for metric display
	 * @param mnode
	 *            the selected Member Node
	 * @param model
	 *            add model view for jsp
	 */
	@RequestMapping(value = "/mn/{mnode}/{timespan}", method = RequestMethod.GET)
	public String getMNMetrics(@PathVariable int timespan,
			@PathVariable String mnode, Model model) {
		Map<String, ArrayList<MonthlyMetric>> MNMap = new HashMap<String, ArrayList<MonthlyMetric>>();
		try {
			MNMap = DataCache.INSTANCE.getMNDataMap(CN_URL);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ArrayList<MonthlyMetric> total = new ArrayList<MonthlyMetric>();
		total = MNMap.get("\"" + mnode + "\"");
		ArrayList<String> result = new ArrayList<String>();
		for (int i = 24 - timespan; i < 24; i++) {
			result.add(total.get(i).getMetricValue());
		}
		DateList dl = new DateList(timespan);
		model.addAttribute("date", dl.getDatelist());
		model.addAttribute("data", result);
		model.addAttribute("mnode", "'" + mnode.substring(9, mnode.length())
				+ "'");
		return "MNode";
	}
	/** This method is to generate the default document figure frame */
	@RequestMapping(value = "/doc", method = RequestMethod.GET)
	public String getDocFigure() {
		return "DocFigure";
	}
	/**
	 * This method is to get the download number of a certain document.
	 * 
	 * @param identifier
	 *            the document's identifier
	 * @param model
	 *            add model view for jsp
	 */
	@RequestMapping(value = "/dataone_doc/{identifier}", method = RequestMethod.GET)
	public String getDocMetrics(@PathVariable String identifier, Model model) {
		identifier = identifier.replace('|', '/');
		identifier = identifier.replace('&', '.');
		DocMetric doc = new DocMetric();
		doc.setCN_URL(CN_URL);
		ArrayList<MonthlyMetric> total = new ArrayList<MonthlyMetric>();
		ArrayList<String> result = new ArrayList<String>();
		ArrayList<String> date = new ArrayList<String>();
		try {
			total = doc.getDocMetricsByMonth(identifier, 12);
			for (int i = 0; i < 12; i++) {
				System.out.println(total.get(i).getMetricDate());
				result.add(total.get(i).getMetricValue());
				date.add("\"" + total.get(i).getMetricDate().substring(5, 7)
						+ "/" + total.get(i).getMetricDate().substring(2, 4)
						+ "\"");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String total_impact = "dataone:" + identifier;
		model.addAttribute("ti", total_impact);
		model.addAttribute("date", date);
		model.addAttribute("data", result);
		return "DocMetric";
	}
}
